// 红花设置页面
Page({
  data: {
    settings: {
      autoGiveFlower: true,
      homeworkFlower: 1,
      attendanceFlower: 1,
      participationFlower: 1,
      goodBehaviorFlower: 2,
      maxFlowerPerDay: 10,
      flowerRules: [
        '按时完成作业 +1朵',
        '积极参与课堂讨论 +1朵',
        '帮助同学解答问题 +2朵',
        '表现优秀 +2朵'
      ]
    },
    loading: false,
    saving: false
  },

  onLoad() {
    console.log('红花设置页面加载');
    this.loadSettings();
  },

  // 加载设置
  async loadSettings() {
    this.setData({ loading: true });
    
    try {
      // 模拟从服务器加载设置
      await new Promise(resolve => setTimeout(resolve, 500));
      
      this.setData({ loading: false });
    } catch (error) {
      console.error('加载设置失败:', error);
      this.setData({ loading: false });
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    }
  },

  // 切换自动发放红花
  toggleAutoGive(e) {
    this.setData({
      'settings.autoGiveFlower': e.detail.value
    });
  },

  // 作业红花数量变化
  onHomeworkFlowerChange(e) {
    this.setData({
      'settings.homeworkFlower': parseInt(e.detail.value) || 1
    });
  },

  // 出勤红花数量变化
  onAttendanceFlowerChange(e) {
    this.setData({
      'settings.attendanceFlower': parseInt(e.detail.value) || 1
    });
  },

  // 参与红花数量变化
  onParticipationFlowerChange(e) {
    this.setData({
      'settings.participationFlower': parseInt(e.detail.value) || 1
    });
  },

  // 良好行为红花数量变化
  onGoodBehaviorFlowerChange(e) {
    this.setData({
      'settings.goodBehaviorFlower': parseInt(e.detail.value) || 2
    });
  },

  // 每日最大红花数量变化
  onMaxFlowerChange(e) {
    this.setData({
      'settings.maxFlowerPerDay': parseInt(e.detail.value) || 10
    });
  },

  // 添加红花规则
  addFlowerRule() {
    wx.showModal({
      title: '添加红花规则',
      editable: true,
      placeholderText: '请输入红花规则',
      success: (res) => {
        if (res.confirm && res.content.trim()) {
          const rules = [...this.data.settings.flowerRules, res.content.trim()];
          this.setData({
            'settings.flowerRules': rules
          });
        }
      }
    });
  },

  // 删除红花规则
  deleteFlowerRule(e) {
    const index = e.currentTarget.dataset.index;
    const rules = this.data.settings.flowerRules.filter((_, i) => i !== index);
    this.setData({
      'settings.flowerRules': rules
    });
  },

  // 保存设置
  async saveSettings() {
    this.setData({ saving: true });
    
    try {
      // 模拟保存到服务器
      await new Promise(resolve => setTimeout(resolve, 1000));
      
      wx.showToast({
        title: '设置保存成功',
        icon: 'success'
      });
      
    } catch (error) {
      console.error('保存设置失败:', error);
      wx.showToast({
        title: '保存失败，请重试',
        icon: 'none'
      });
    } finally {
      this.setData({ saving: false });
    }
  },

  // 重置设置
  resetSettings() {
    wx.showModal({
      title: '重置设置',
      content: '确定要重置所有红花设置吗？',
      confirmText: '重置',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            settings: {
              autoGiveFlower: true,
              homeworkFlower: 1,
              attendanceFlower: 1,
              participationFlower: 1,
              goodBehaviorFlower: 2,
              maxFlowerPerDay: 10,
              flowerRules: [
                '按时完成作业 +1朵',
                '积极参与课堂讨论 +1朵',
                '帮助同学解答问题 +2朵',
                '表现优秀 +2朵'
              ]
            }
          });
          
          wx.showToast({
            title: '设置已重置',
            icon: 'success'
          });
        }
      }
    });
  }
});




